//
//  subCommentTableViewCell.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/2.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import TYAttributedLabel
class SubCommentTableViewCell: UITableViewCell {

    @IBOutlet weak var commentLabel: TYAttributedLabel!
    @IBOutlet weak var commentLabelHeight: NSLayoutConstraint!
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        // Initialization code
    }
    
    private func setUI() {
        commentLabel.textColor = ONColor.subTextColor
        commentLabel.linesSpacing = LinesSpacing
        commentLabel.font = UIFont.systemFont(ofSize: 12)
        
        let longTap = UILongPressGestureRecognizer(target: self, action: #selector(longTaps))
        self.contentView.addGestureRecognizer(longTap)
    }
    
    @objc func longTaps() {
        self.becomeFirstResponder()
        let menu = UIMenuController.shared
        menu.setTargetRect(self.contentView.bounds, in: self.contentView)
        let copy = UIMenuItem(title: "复制", action: #selector(copyAction(_:)))
        let report = UIMenuItem(title: "举报", action: #selector(reportAction(_:)))
        menu.menuItems = [copy, report]
        menu.isMenuVisible = true;
        
    }
    
    @objc func copyAction(_ menu: UIMenuController) {
        let pasteboard = UIPasteboard.general
        pasteboard.string = commentLabel.text
    }
    
    @objc func reportAction(_ menu: UIMenuController) {
        ReportView().show(contnets: ["发布不正当内容对我造成骚扰", "存在欺诈骗钱行为", "此账号可能被盗用", "发布仿冒信息"], type: .report) { (index, view) in
            DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 1, execute: {
                DispatchQueue.main.async {
                    view.hideAndShow()
                }
            })
        }
    }
    
    override var canBecomeFirstResponder: Bool {
        return true
    }
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if  action == #selector(copyAction) || action == #selector(reportAction) {
            return true
        } else {
            return false
        }
    }
    
    func setData(contentOffset: CGFloat, comment: myCommendModel, cellModel: myCommendModel) {
        
        if let nickname = comment.commentator{
            
            commentLabel.text = nickname + " 回复 " + (cellModel.commentUser?.nickname!)! + " " + comment.commentContent!
        }
        else{
            commentLabel.text = "没昵称用户 回复 " + (cellModel.commentUser?.nickname!)! + " " + comment.commentContent!
        }
        commentLabelHeight.constant = CGFloat(commentLabel.getHeightWithWidth(ScreenWidth - contentOffset)
        )
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
